package com.ptc.ims.client.view;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import com.ptc.ims.client.presenter.ProductListPresenter.Display;
import com.ptc.ims.shared.Product;

public class ProductListView 
extends Composite
implements Display
{
	private final FlexTable contentTable;
	private final FlexTable productsTable;	
	private final Button deleteButton;
	private final Button createButton;
	private final Button addButton;
	private final Button editButton;
	private final TextBox productName;
	private final TextBox price;

	public ProductListView()
	{
		DecoratorPanel mainPanelDecorator = new DecoratorPanel();
		mainPanelDecorator.addStyleName("main");
		initWidget(mainPanelDecorator);
		mainPanelDecorator.setWidth("25em");		

		contentTable = new FlexTable();
		contentTable.setWidth("100%");
		contentTable.getCellFormatter().addStyleName(0, 0, "productlist");
		contentTable.getCellFormatter().setWidth(0, 0, "100%");
		contentTable.getFlexCellFormatter().setVerticalAlignment(0, 0, DockPanel.ALIGN_TOP);

		HorizontalPanel buttonPanel = new HorizontalPanel();		
		deleteButton = new Button("Delete");
		createButton = new Button("New Product");
		editButton = new Button("Edit");
		deleteButton.setVisible(false);
		editButton.setVisible(false);		
		buttonPanel.add(createButton);
		buttonPanel.add(editButton);
		buttonPanel.add(deleteButton);
		buttonPanel.addStyleName("buttons");
		contentTable.setWidget(0, 0, buttonPanel);

		productName = new TextBox();
		price = new TextBox();
		addButton = new Button("Add");

		productsTable = new FlexTable();
		productsTable.setCellSpacing(0);
		productsTable.setCellPadding(0);
		productsTable.setWidth("100%");
		productsTable.addStyleName("products");
		productsTable.getColumnFormatter().setWidth(0, "15px");
		contentTable.setWidget(1, 0, productsTable);
		
		HorizontalPanel addPanel = new HorizontalPanel();
		addPanel.add(addButton);
		addPanel.add(productName);
		addPanel.add(price);
		buttonPanel.addStyleName("buttons");
		contentTable.setWidget(2, 0, addPanel);

		mainPanelDecorator.add(contentTable);
	}	

	/**  Overriden Methods from ProductsPresenter.Display */

	@Override
	public HasClickHandlers getAddButton() 
	{		
		return addButton;
	}

	@Override
	public HasClickHandlers getCreateButton()
	{
		return createButton;
	}

	@Override
	public HasClickHandlers getEditButton()
	{		
		return editButton;
	}

	@Override
	public HasClickHandlers getDeleteButton()
	{		
		return deleteButton;
	}	

	@Override
	public HasClickHandlers getList()
	{		
		return productsTable;
	}

	@Override
	public Widget asWidget()
	{
		return this;
	}

	@Override
	public HasValue<String> getProductName() 
	{		
		return productName;
	}

	@Override
	public HasValue<String> getPrice()
	{		
		return price;
	}

	@Override
	public void setData(List<Product> products)
	{
		productsTable.removeAllRows();
		Product product = null;
		CheckBox checkBox = null;
		NumberFormat fmt = NumberFormat.getCurrencyFormat("PHP");
		for (int i = 0; i < products.size(); ++i) 
		{			
			product = products.get(i);
			checkBox = new CheckBox();
			checkBox.setFormValue(product.getId());
			productsTable.setWidget(i, 0, checkBox);
			productsTable.setText(i, 1, product.getName());
			productsTable.setText(i, 2, fmt.format(product.getPrice()));
			productsTable.getElement().setId(product.getId());
		}
		
	}

	@Override
	public int[] getDoubleClickedCell(ClickEvent event)
	{		
		int[] cellCoodinate = new int[2];
		HTMLTable.Cell cell = productsTable.getCellForEvent(event);

		if (cell != null)
		{
			if (cell.getCellIndex() > 0) 
			{
				cellCoodinate[0] = cell.getRowIndex();
				cellCoodinate[1] = cell.getCellIndex();
			}
		}
		return cellCoodinate;
	}

	@Override
	public List<String> getSelectedRows() 
	{
		List<String> selectedRows = new ArrayList<String>();

		for (int i = 0; i < productsTable.getRowCount(); ++i) 
		{
			CheckBox checkBox = (CheckBox) productsTable.getWidget(i, 0);
			if (checkBox.getValue()) 
			{
				selectedRows.add(checkBox.getFormValue());
			}
		}
		return selectedRows;
	}

	@Override
	public void removeSelectedRows(List<String> productIds)
	{
		for (String productId : productIds)
		{
			for (int i = 0; i < productsTable.getRowCount(); ++i) 
			{
				CheckBox checkBox = (CheckBox) productsTable.getWidget(i, 0);
				if (checkBox.getFormValue().equals(productId)) 
				{
					productsTable.removeRow(i);
				}
			}
		}
	}

	@Override
	public void addNewRow(Product product) 
	{
		NumberFormat fmt = NumberFormat.getCurrencyFormat("PHP");
		CheckBox checkBox = new CheckBox();
		checkBox.setFormValue(product.getId());
		int row = productsTable.getRowCount();
		productsTable.setWidget(row, 0,checkBox);
		productsTable.setText(row, 1, product.getName());
		productsTable.setText(row,2, fmt.format(product.getPrice()));
		productName.setValue("");
		price.setValue("");
	}

	@Override
	public void showMoreOptionsButton(ClickEvent event) 
	{
		if (event != null)
		{
			HTMLTable.Cell cell = productsTable.getCellForEvent(event);

			if (cell != null && cell.getCellIndex() == 0) 
			{	      
				editButton.setVisible(getSelectedRows().size() == 1);
				deleteButton.setVisible(getSelectedRows().size() > 0);
			}	
		}
		else
		{
			editButton.setVisible(getSelectedRows().size() == 1);
			deleteButton.setVisible(getSelectedRows().size() > 0);
		}
	}		
}

